package ui.mainscreen;

import java.awt.BorderLayout;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JPanel;

import ui.ScreenPanel;
import ui.mainscreen.listeners.CreatorsButtonListener;
import ui.mainscreen.listeners.ExitButtonListener;
import ui.mainscreen.listeners.LoadButtonListener;
import ui.mainscreen.listeners.LoadPanelListener;
import ui.mainscreen.listeners.NewGameButtonListener;
import ui.mainscreen.listeners.OptionButtonListener;
import ui.mainscreen.listeners.OptionPanelListener;

public class MainScreenPanel extends ScreenPanel {

	public final MenuPanel menuPanel = new MenuPanel();
	public final TitlePanel titlePanel = new TitlePanel();
	public final VersionPanel versionPanel = new VersionPanel();
	public final JPanel eastPanel = new JPanel();
	public final LoadPanel loadPanel = new LoadPanel();
	public final OptionPanel optionPanel = new OptionPanel();

	/* Реализация шаблона - Синглтон */
	private static MainScreenPanel instance;

	/**
	 * @return the instance
	 */
	public static synchronized MainScreenPanel getInstance() {
		if (instance == null) {
			instance = new MainScreenPanel();
		}
		return instance;
	}

	private MainScreenPanel() {

		setOnCreateProperties("image/bgd.jpg");

		/* Устанока прозрачности панелей */
		titlePanel.setOpaque(false);
		versionPanel.setOpaque(false);
		eastPanel.setOpaque(false);

		/* Создание пустых границ панелей */
		titlePanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
		versionPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
		eastPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));

		/* Добавление панелей главного окна */
		add(menuPanel, BorderLayout.WEST);
		add(titlePanel, BorderLayout.NORTH);
		add(versionPanel, BorderLayout.SOUTH);
		add(eastPanel, BorderLayout.EAST);

		/* Создание распорки для Восточной панели */
		BoxLayout boxEast = new BoxLayout(eastPanel, BoxLayout.X_AXIS);
		eastPanel.setLayout(boxEast);
		eastPanel.add(Box.createHorizontalStrut(100));

		/* Добавляем блок слушателей */
		addListeners();

	}

	@Override
	protected void addListeners() {
		/* Вызов панели опций новой игры */
		menuPanel.menuNewButton.addActionListener(new NewGameButtonListener());

		/* Вызов панели загрузок */
		menuPanel.menuLoadButton.addActionListener(new LoadButtonListener(loadPanel, menuPanel,
			this));
		loadPanel.okLoadBut.addActionListener(new LoadPanelListener(loadPanel, menuPanel));

		/* Вызов панели опций */
		menuPanel.menuOptionButton.addActionListener(new OptionButtonListener(optionPanel,
			menuPanel, this));
		optionPanel.okOptionBut.addActionListener(new OptionPanelListener(optionPanel, menuPanel));

		/* Вызов панели создателей */
		menuPanel.menuCreatorButton.addActionListener(new CreatorsButtonListener());

		/* Выход из игры */
		menuPanel.menuExitButton.addActionListener(new ExitButtonListener());
	}
}
